package com.nwpu.server.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nwpu.server.admin.dao.PipelineDao;
import com.nwpu.server.admin.dao.PipelineTitleDao;
import com.nwpu.server.admin.entity.PipelineEntity;
import com.nwpu.server.admin.entity.PipelineTitleEntity;
import com.nwpu.server.admin.service.PipelineService;
import com.nwpu.server.admin.service.PipelineTitleService;
import com.nwpu.server.common.response.PageUtils;
import com.nwpu.server.common.response.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("pipelineTitleService")
public class PipelineTitleServiceImpl extends ServiceImpl<PipelineTitleDao, PipelineTitleEntity> implements PipelineTitleService {

    @Autowired
    PipelineDao pipelineDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PipelineTitleEntity> page = this.page(
                new Query<PipelineTitleEntity>().getPage(params),
                new QueryWrapper<PipelineTitleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Cacheable(value = {"admin:pipelineMethod"},key = "#root.method.name",sync = true)
    public List<PipelineTitleEntity> getAll() {
        List<PipelineTitleEntity> all = baseMapper.selectList(null);
        for (PipelineTitleEntity pipelineTitleEntity : all) {
            Long id = pipelineTitleEntity.getId();
            QueryWrapper<PipelineEntity> title_id = new QueryWrapper<PipelineEntity>().eq("title_id", id);
            List<PipelineEntity> pipelineEntities = pipelineDao.selectList(title_id);
            pipelineTitleEntity.setNames(pipelineEntities.stream().parallel().map(PipelineEntity::getPipelineName).collect(Collectors.toList()));
        }
        return all;
    }

}